package com.sixth.controller;

import com.sixth.annotation.OperationLogAdvice;
import com.sixth.bean.MarketAdmin;
import com.sixth.bean.bo.AdminDeleteBO;
import com.sixth.bean.bo.AdminListBO;
import com.sixth.bean.common.BasePageInfo;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.vo.AdminCreateVO;
import com.sixth.bean.vo.AdminListVO;
import com.sixth.constant.Constant;
import com.sixth.service.AdminAdminService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("admin/admin")
public class AdminAdminController {
    @Autowired
    AdminAdminService adminService;

    //    http://182.92.235.201:8083/admin/admin/list?page=1&limit=20&sort=add_time&order=desc
    @RequiresPermissions(value = "admin:admin:list")
    @RequestMapping("list")
    public BaseRespVo list(AdminListBO adminListBO, String username) {

        AdminListVO adminListVO = adminService.list(adminListBO,username);

        return BaseRespVo.ok(adminListVO);
    }


    //http://182.92.235.201:8083/admin/admin/delete
    //{"id":20,"username":"ert123","avatar":"'","roleIds":[3]}
    //{"errno":0,"errmsg":"成功"}
    @OperationLogAdvice(operationType = 1,operation = "删除管理员")
    @RequiresPermissions(value = "admin:admin:delete")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody AdminDeleteBO adminDeleteBO) {
        //表面上是删除管理员，实际上是将管理员的状态位修改为1

       int code = adminService.deleteByPrimaryKey(adminDeleteBO.getId());
       if(code == Constant.NON){
           return BaseRespVo.invalidData();
       }
        System.out.println(adminDeleteBO.toString());
       return BaseRespVo.ok();




    }

    @OperationLogAdvice(operationType = 1,operation = "编辑管理员")
    @RequiresPermissions(value = "admin:admin:update")
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketAdmin admin) {

        adminService.modify(admin);

        return BaseRespVo.ok();

    }


    @OperationLogAdvice(operationType = 1,operation = "新增管理员")
    @RequiresPermissions(value = "admin:admin:create")
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody MarketAdmin admin) {

        MarketAdmin marketAdmin = adminService.create(admin);
        AdminCreateVO adminCreateVO = new AdminCreateVO();
        BeanUtils.copyProperties(marketAdmin, adminCreateVO);

        return  BaseRespVo.ok(marketAdmin);


    }

}
